﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.Linq;
using System.Configuration;
using Kronos.Classes;
using System.Net.Mail;
using System.Net;
using Kronos.DatabaseModels;

namespace Kronos.Classes
{
    public class RegistrationModel:BaseUserModel
    {
       
        protected string PasswordOne;
        protected string PasswordTwo;
        protected string Email;
        protected string IdNumber;
        
        public RegistrationModel(string Username,Label registrationNotice,string pwd1,string pwd2,string email,string studentnumber)
            :base(Username,registrationNotice)
        {           
                this.PasswordOne = pwd1;
                this.PasswordTwo = pwd2;
                this.Email= email;
        
                this.IdNumber = studentnumber;

                if (validRegistration())
                {
                    newUser.username = UserIn;
                    newUser.hashedPassword = base.EncryptPwd(pwd1);
                    newUser.email = Email;
                    newUser.studentID= IdNumber;
                    newUser.suspended = 0;
                    newUser.user_type = 0;
                    base.AddUserInDatabase();
                    base.SaveTheDatabase();
                    base.WriteMessageLabel("Thank You For registering, You Should Recieve an email!");
                    
                    SendRegistrationEmail();
                }
                else
                {
                    base.WriteMessageLabel("Invalid Data Entered. Cannot add user.");
                }

         
        }

        public virtual bool validRegistration()
        {
            if (checkPasswordValidation() && checkIdNumberInDatabase()
                && checkEmailInDatabase() && checkUserInDatabase())
                return true;
            else
                return false;


        }

        public virtual bool checkPasswordValidation()
        {
            if (PasswordOne == "" || PasswordTwo == "")
            {
                base.WriteMessageLabel("Do not leave password fields blank");
                return false;
            }
            else if (PasswordOne != PasswordTwo)
            {
                base.WriteMessageLabel("Passwords do not match");
                return false;
            }
            else
                return true;

        }
        public virtual bool checkUserInDatabase()
        {
            var username_in_dB = (from a_username in soen341dB_context.Users
                                  where a_username.username == UserIn
                                  select a_username).FirstOrDefault();
            if (username_in_dB != null)
            {
                base.WriteMessageLabel("Username Exists");
                return false;
            }
            else
            {
                return true;
            }



        }
        public virtual bool checkEmailInDatabase()
        {
            var email_in_dB = (from an_email in soen341dB_context.Users
                               where an_email.email == Email
                               select an_email).FirstOrDefault();
            if (email_in_dB != null)
            {
                base.WriteMessageLabel("Email already registered in database");
                return false;
            }
            else
            {
                return true;
            }



        }

        public virtual bool checkIdNumberInDatabase()
        {
            var studentid_in_dB = (from a_student_id in soen341dB_context.Users
                                   where a_student_id.studentID ==IdNumber
                                   select a_student_id).FirstOrDefault();
            if (studentid_in_dB != null)
            {
                base.WriteMessageLabel("Student ID number already registered");
                return false;
            }
            else
            {
                return true;
            }


        }
        public virtual void SendRegistrationEmail()
        {
            Notification.Visible = true;
            MailMessage mail = new MailMessage();
            mail.To.Add(Email);
            mail.From = new MailAddress("admin@341.atsebak.com");
            mail.Subject = "mYScheduler Registration Confirmation";
            mail.Body = "Thank You for registering to the Concordia University mYScheduler. Your username is: " + UserIn + " Please report bugs and suggestions to admin@341.atsebak.com";
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "mail.atsebak.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = new NetworkCredential("admin@341.atsebak.com", "softwareprocess");
            smtp.EnableSsl = false;
            smtp.Send(mail);

        }



    }
}